/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.stx.xhb.customwaterview.slice;

import com.stx.xhb.customwaterview.ResourceTable;
import com.stx.xhb.customwaterview.components.WaterFlake;
import com.stx.xhb.customwaterview.model.WaterModel;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.ToastDialog;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.ArrayList;

/**
 * JAVA版蚂蚁森林demo
 *
 * @since 2021-06-08
 */
public class ForestAbilitySlice extends AbilitySlice {
    private WaterFlake mWaterFlake;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_forest);
    }

    @Override
    public void onActive() {
        super.onActive();
        mWaterFlake = (WaterFlake) findComponentById(ResourceTable.Id_waterFlake);
        Button btnRest = (Button) findComponentById(ResourceTable.Id_btnRest);
        btnRest.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                mWaterFlake.resetComponent();
            }
        });
        mWaterFlake.setOnWaterItemListener(new WaterFlake.OnWaterItemListener() {
            @Override
            public void onItemClick(WaterModel pos) {
                new ToastDialog(getContext()).setText("点击了" + pos.getContent()).show();
            }
        });

        initData();
    }

    private void initData() {
        ArrayList<WaterModel> mModelList = new ArrayList<>();
        final int count = 6;
        for (int index = 0; index < count; index++) {
            mModelList.add(new WaterModel(index + count + "g"));
        }
        mWaterFlake.setModelList(mModelList);

        new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
            @Override
            public void run() {
                // 此处目前写死坐标，后期可以获取小树的坐标添加进去
                final int desY = 1200;
                mWaterFlake.updateCollectionPosition(0, desY);
            }
        });
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
